# services/file_service.py
# Encapsulates local file system reading logic (primarily for the whiteboard feature).
# Detailed implementation will follow based on design documents.

import os
from typing import Optional

class FileService:
    def __init__(self):
        print("FileService initialized.")

    def read_file_content(self, file_path: str) -> tuple[Optional[str], Optional[str]]:
        """
        Reads the content of a file.
        Returns a tuple: (content, error_message).
        If successful, content is a string and error_message is None.
        If failed, content is None and error_message is a string.
        """
        print(f"FileService attempting to read file: {file_path}")
        try:
            if not os.path.exists(file_path):
                return None, f"File not found: {file_path}"
            if not os.path.isfile(file_path):
                return None, f"Path is not a file: {file_path}"
            
            # MVP: Simple size check (e.g., 5MB limit)
            # file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
            # if file_size_mb > 5: # Example limit
            #     return None, f"File exceeds size limit (5MB): {file_path}"

            # MVP: Assume text file, handle potential encoding issues simply
            with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
                content = f.read()
            return content, None
        except PermissionError:
            return None, f"Permission denied to read file: {file_path}"
        except Exception as e:
            return None, f"Error reading file {file_path}: {str(e)}"

if __name__ == '__main__':
    fs = FileService()
    
    # Create a dummy file for testing
    dummy_file_path = "test_whiteboard_file.txt"
    with open(dummy_file_path, "w", encoding="utf-8") as f:
        f.write("Hello from the whiteboard test file!\nThis is the second line.")
    
    content, error = fs.read_file_content(dummy_file_path)
    if error:
        print(f"Error reading dummy file: {error}")
    else:
        print(f"Dummy file content:\n{content}")
    
    os.remove(dummy_file_path) # Clean up

    non_existent_content, non_existent_error = fs.read_file_content("non_existent_file.txt")
    if non_existent_error:
        print(f"Error for non-existent file (expected): {non_existent_error}")

    print("FileService example run complete.")